home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / c / STRINGLib.lha / STRINGLib / Developer / source / TestStringLib.c next >
Encoding:
C/C++ Source or Header  |  1999-08-08  |  20.0 KB  |  649 lines

  1. /*** excluded for vbcc, see below
  2. *MP* _cli_parse(){return;}
  3. *MP* _wb_parse(){return;}
  4. ***/
  5.  
  6. /*
  7.  * Test program for string(3) routines.
  8.  * 
  9.  * Note that at least one Bell Labs implementation of the string
  10.  * routines flunks a couple of these tests -- the ones which test
  11.  * behavior on "negative" characters.
  12.  */
  13.  
  14. /* 10.12.1998 Michaela Prüß, patches vor actual stringlib and vbcc
  15.  *
  16.  * see patches direct in source. documented in comments after *MP*
  17.  * most incomplete declarations (must corrected for useing prototypes
  18.  *
  19.  */
  20.  
  21. #define VBCC        /*MP* lock in stringlib.h for more detail */
  22.  
  23. #include <stdio.h>
  24. #include "stringlib.h"
  25.  
  26. #define    STREQ(a, b)    (Strcmp((a), (b)) == 0)
  27.  
  28. char *it = "<UNSET>";        /* Routine name for message routines. */
  29. int waserror = 0;        /* For exit status. */
  30.  
  31. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  32. int charsigned;            /* Result. */
  33.  
  34. /*
  35.  - check - complain if condition is not true
  36.  -
  37. *MP* no return also uses void, not int
  38. *MP* int number must printed with %d, not %ld
  39.  -
  40.  */
  41.  
  42. void check(int thing, int number)            /* Test number for error message. */
  43. {
  44.     if (!thing) {
  45.         printf("%s flunked test %d\n", it, number);
  46.         waserror = 1;
  47.     }
  48. }
  49.  
  50. /*
  51.  - equal - complain if first two args don't Strcmp as equal
  52.  -
  53. *MP* no return also uses void, not int
  54.  -
  55.  */
  56.  
  57. void equal(char *a, char *b, int number)            /* Test number for error message. */
  58. {
  59.     check(a != NULL && b != NULL && STREQ(a, b), number);
  60. }
  61.  
  62. char one[50];
  63. char two[50];
  64.  
  65. #ifdef UNIXERR
  66. #define ERR 1
  67. #endif
  68. #ifdef BERKERR
  69. #define ERR 1
  70. #endif
  71. #ifdef ERR
  72. int f;
  73. #ifdef unix
  74. extern char *sys_errlist[];
  75. extern int sys_nerr;
  76. #else
  77. char *sys_errlist[1] = {"dummy entry to keep compilers happy"};
  78. int sys_nerr = 1;
  79. #endif
  80. extern int errno;
  81. #endif
  82.  
  83. /* ARGSUSED
  84.  -
  85. *MP* prototyp without any returntype, added int
  86.  -
  87.  */
  88.  
  89. int main(int argc, char *argv[])
  90. {
  91.     /*
  92.      * First, establish whether chars are signed.
  93.      */
  94.     if (uctest[0] < uctest[1])
  95.         charsigned = 0;
  96.     else
  97.         charsigned = 1;
  98.  
  99.     /*
  100.      * Then, do the rest of the work.  Split into two functions because
  101.      * some compilers get unhappy about a single immense function.
  102.      */
  103.     first();
  104.     second();
  105.  
  106.     exit((waserror) ? 1 : 0);
  107. }
  108.  
  109. /*MP* added void */
  110.  
  111. void first()
  112. {
  113.     /*
  114.      * Test Strcmp first because we use it to test other things.
  115.      */
  116.     it = "Strcmp";
  117.     check(Strcmp("", "") == 0, 1);        /* Trivial case. */
  118.     check(Strcmp("a", "a") == 0, 2);    /* Identity. */
  119.     check(Strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  120.     check(Strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  121.     check(Strcmp("abcd", "abc") > 0, 5);
  122.     check(Strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  123.     check(Strcmp("abce", "abcd") > 0, 7);
  124.     check(Strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  125.     if (charsigned)                /* Sign-bit comparison. */
  126.         check(Strcmp("a\203", "a\003") < 0, 9);
  127.     else
  128.         check(Strcmp("a\203", "a\003") > 0, 9);
  129.  
  130.     /*
  131.      * Test Strcpy next because we need it to set up other tests.
  132.      */
  133.     it = "Strcpy";
  134.     check(Strcpy(one, "abcd") == one, 1);    /* Returned value. */
  135.     equal(one, "abcd", 2);            /* Basic test. */
  136.  
  137.     /*int*/ Strcpy(one, "x");
  138.     equal(one, "x", 3);            /* Writeover. */
  139.     equal(one+2, "cd", 4);            /* Wrote too much? */
  140.  
  141.     /*void*/ Strcpy(two, "hi there");
  142.     /*void*/ Strcpy(one, two);
  143.     equal(one, "hi there", 5);        /* Basic test encore. */
  144.     equal(two, "hi there", 6);        /* Stomped on source? */
  145.  
  146.     /*void*/ Strcpy(one, "");
  147.     equal(one, "", 7);            /* Boundary condition. */
  148.  
  149.     /*
  150.      * Strcat
  151.      */
  152.     it = "Strcat";
  153.     /*void*/ Strcpy(one, "ijk");
  154.     check(Strcat(one, "lmn") == one, 1);    /* Returned value. */
  155.     equal(one, "ijklmn", 2);        /* Basic test. */
  156.  
  157.     /*void*/ Strcpy(one, "x");
  158.     /*void*/ Strcat(one, "yz");
  159.     equal(one, "xyz", 3);            /* Writeover. */
  160.     equal(one+4, "mn", 4);            /* Wrote too much? */
  161.  
  162.     /*void*/ Strcpy(one, "gh");
  163.     /*void*/ Strcpy(two, "ef");
  164.     /*void*/ Strcat(one, two);
  165.     equal(one, "ghef", 5);            /* Basic test encore. */
  166.     equal(two, "ef", 6);            /* Stomped on source? */
  167.  
  168.     /*void*/ Strcpy(one, "");
  169.     /*void*/ Strcat(one, "");
  170.     equal(one, "", 7);            /* Boundary conditions. */
  171.     /*void*/ Strcpy(one, "ab");
  172.     /*void*/ Strcat(one, "");
  173.     equal(one, "ab", 8);
  174.     /*void*/ Strcpy(one, "");
  175.     /*void*/ Strcat(one, "cd");
  176.     equal(one, "cd", 9);
  177.  
  178.     /*
  179.      * Strncat - first test it as Strcat, with big counts, then
  180.      * test the count mechanism.
  181.      */
  182.     it = "Strncat";
  183.     /*void*/ Strcpy(one, "ijk");
  184.     check(Strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  185.     equal(one, "ijklmn", 2);        /* Basic test. */
  186.  
  187.     /*void*/ Strcpy(one, "x");
  188.     /*void*/ Strncat(one, "yz", 99);
  189.     equal(one, "xyz", 3);            /* Writeover. */
  190.     equal(one+4, "mn", 4);            /* Wrote too much? */
  191.  
  192.     /*void*/ Strcpy(one, "gh");
  193.     /*void*/ Strcpy(two, "ef");
  194.     /*void*/ Strncat(one, two, 99);
  195.     equal(one, "ghef", 5);            /* Basic test encore. */
  196.     equal(two, "ef", 6);            /* Stomped on source? */
  197.  
  198.     /*void*/ Strcpy(one, "");
  199.     /*void*/ Strncat(one, "", 99);
  200.     equal(one, "", 7);            /* Boundary conditions. */
  201.     /*void*/ Strcpy(one, "ab");
  202.     /*void*/ Strncat(one, "", 99);
  203.     equal(one, "ab", 8);
  204.     /*void*/ Strcpy(one, "");
  205.     /*void*/ Strncat(one, "cd", 99);
  206.     equal(one, "cd", 9);
  207.  
  208.     /*void*/ Strcpy(one, "ab");
  209.     /*void*/ Strncat(one, "cdef", 2);
  210.     equal(one, "abcd", 10);            /* Count-limited. */
  211.  
  212.     /*void*/ Strncat(one, "gh", 0);
  213.     equal(one, "abcd", 11);            /* Zero count. */
  214.  
  215.     /*void*/ Strncat(one, "gh", 2);
  216.     equal(one, "abcdgh", 12);        /* Count and length equal. */
  217.  
  218.     /*
  219.      * Strncmp - first test as Strcmp with big counts, then test
  220.      * count code.
  221.      */
  222.     it = "Strncmp";
  223.     check(Strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  224.     check(Strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  225.     check(Strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  226.     check(Strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  227.     check(Strncmp("abcd", "abc", 99) > 0, 5);
  228.     check(Strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  229.     check(Strncmp("abce", "abcd", 99) > 0, 7);
  230.     check(Strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  231.     if (charsigned)                /* Sign-bit comparison. */
  232.         check(Strncmp("a\203", "a\003", 2) < 0, 9);
  233.     else
  234.         check(Strncmp("a\203", "a\003", 2) > 0, 9);
  235.     check(Strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  236.     check(Strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  237.     check(Strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  238.     check(Strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  239.  
  240.     /*
  241.      * Strncpy - testing is a bit different because of odd semantics
  242.      */
  243.     it = "Strncpy";
  244.     check(Strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  245.     equal(one, "abc", 2);            /* Did the copy go right? */
  246.  
  247.     /*void*/ Strcpy(one, "abcdefgh");
  248.     /*void*/ Strncpy(one, "xyz", 2);
  249.     equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  250.  
  251.     /*void*/ Strcpy(one, "abcdefgh");
  252.     /*void*/ Strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  253.     equal(one, "xyzdefgh", 4);
  254.  
  255.     /*void*/ Strcpy(one, "abcdefgh");
  256.     /*void*/ Strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  257.     equal(one, "xyz", 5);
  258.     equal(one+4, "efgh", 6);        /* Wrote too much? */
  259.  
  260.     /*void*/ Strcpy(one, "abcdefgh");
  261.     /*void*/ Strncpy(one, "xyz", 5);        /* Copy includes padding. */
  262.     equal(one, "xyz", 7);
  263.     equal(one+4, "", 8);
  264.     equal(one+5, "fgh", 9);
  265.  
  266.     /*void*/ Strcpy(one, "abc");
  267.     /*void*/ Strncpy(one, "xyz", 0);        /* Zero-length copy. */
  268.     equal(one, "abc", 10);    
  269.  
  270.     /*void*/ Strncpy(one, "", 2);        /* Zero-length source. */
  271.     equal(one, "", 11);
  272.     equal(one+1, "", 12);    
  273.     equal(one+2, "c", 13);
  274.  
  275.     /*void*/ Strcpy(one, "hi there");
  276.     /*void*/ Strncpy(two, one, 9);
  277.     equal(two, "hi there", 14);        /* Just paranoia. */
  278.     equal(one, "hi there", 15);        /* Stomped on source? */
  279.  
  280.     /*
  281.      * Strlen
  282.      */
  283.     it = "Strlen";
  284.     check(Strlen("") == 0, 1);        /* Empty. */
  285.     check(Strlen("a") == 1, 2);        /* Single char. */
  286.     check(Strlen("abcd") == 4, 3);        /* Multiple chars. */
  287.  
  288.     /*
  289.      * Strchr
  290.      */
  291.     it = "Strchr";
  292.     check(Strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  293.     /*void*/ Strcpy(one, "abcd");
  294.     check(Strchr(one, 'c') == one+2, 2);    /* Basic test. */
  295.     check(Strchr(one, 'd') == one+3, 3);    /* End of String. */
  296.     check(Strchr(one, 'a') == one, 4);    /* Beginning. */
  297.     check(Strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  298.     /*void*/ Strcpy(one, "ababa");
  299.     check(Strchr(one, 'b') == one+1, 6);    /* Finding first. */
  300.     /*void*/ Strcpy(one, "");
  301.     check(Strchr(one, 'b') == NULL, 7);    /* Empty String. */
  302.     check(Strchr(one, '\0') == one, 8);    /* NUL in empty String. */
  303.  
  304.     /*
  305.      * Index - just like Strchr
  306.      */
  307.     it = "Index";
  308.     check(Index("abcd", 'z') == NULL, 1);    /* Not found. */
  309.     /*void*/ Strcpy(one, "abcd");
  310.     check(Index(one, 'c') == one+2, 2);    /* Basic test. */
  311.     check(Index(one, 'd') == one+3, 3);    /* End of String. */
  312.     check(Index(one, 'a') == one, 4);    /* Beginning. */
  313.     check(Index(one, '\0') == one+4, 5);    /* Finding NUL. */
  314.     /*void*/ Strcpy(one, "ababa");
  315.     check(Index(one, 'b') == one+1, 6);    /* Finding first. */
  316.     /*void*/ Strcpy(one, "");
  317.     check(Index(one, 'b') == NULL, 7);    /* Empty String. */
  318.     check(Index(one, '\0') == one, 8);    /* NUL in empty String. */
  319.  
  320.     /*
  321.      * Strrchr
  322.      */
  323.     it = "Strrchr";
  324.     check(Strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  325.     /*void*/ Strcpy(one, "abcd");
  326.     check(Strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  327.     check(Strrchr(one, 'd') == one+3, 3);    /* End of String. */
  328.     check(Strrchr(one, 'a') == one, 4);    /* Beginning. */
  329.     check(Strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  330.     /*void*/ Strcpy(one, "ababa");
  331.     check(Strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  332.     /*void*/ Strcpy(one, "");
  333.     check(Strrchr(one, 'b') == NULL, 7);    /* Empty String. */
  334.     check(Strrchr(one, '\0') == one, 8);    /* NUL in empty String. */
  335.  
  336.     /*
  337.      * Rindex - just like Strrchr
  338.      */
  339.     it = "Rindex";
  340.     check(Rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  341.     /*void*/ Strcpy(one, "abcd");
  342.     check(Rindex(one, 'c') == one+2, 2);    /* Basic test. */
  343.     check(Rindex(one, 'd') == one+3, 3);    /* End of String. */
  344.     check(Rindex(one, 'a') == one, 4);    /* Beginning. */
  345.     check(Rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  346.     /*void*/ Strcpy(one, "ababa");
  347.     check(Rindex(one, 'b') == one+3, 6);    /* Finding last. */
  348.     /*void*/ Strcpy(one, "");
  349.     check(Rindex(one, 'b') == NULL, 7);    /* Empty String. */
  350.     check(Rindex(one, '\0') == one, 8);    /* NUL in empty String. */
  351. }
  352.  
  353. /*MP* added void */
  354.  
  355. void second()
  356. {
  357.     /*
  358.      * Strpbrk - somewhat like Strchr
  359.      */
  360.     it = "Strpbrk";
  361.     check(Strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  362.     /*void*/ Strcpy(one, "abcd");
  363.     check(Strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  364.     check(Strpbrk(one, "d") == one+3, 3);    /* End of String. */
  365.     check(Strpbrk(one, "a") == one, 4);    /* Beginning. */
  366.     check(Strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  367.     check(Strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  368.     /*void*/ Strcpy(one, "abcabdea");
  369.     check(Strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  370.     check(Strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  371.     check(Strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  372.     /*void*/ Strcpy(one, "");
  373.     check(Strpbrk(one, "bc") == NULL, 10);    /* Empty String. */
  374.     check(Strpbrk(one, "") == NULL, 11);    /* Both Strings empty. */
  375.  
  376.     /*
  377.      * Strstr - somewhat like Strchr
  378.      */
  379.     it = "Strstr";
  380.     check(Strstr("abcd", "z") == NULL, 1);    /* Not found. */
  381.     check(Strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  382.     /*void*/ Strcpy(one, "abcd");
  383.     check(Strstr(one, "c") == one+2, 3);    /* Basic test. */
  384.     check(Strstr(one, "bc") == one+1, 4);    /* Multichar. */
  385.     check(Strstr(one, "d") == one+3, 5);    /* End of String. */
  386.     check(Strstr(one, "cd") == one+2, 6);    /* Tail of String. */
  387.     check(Strstr(one, "abc") == one, 7);    /* Beginning. */
  388.     check(Strstr(one, "abcd") == one, 8);    /* Exact match. */
  389.     check(Strstr(one, "abcde") == NULL, 9);    /* Too long. */
  390.     check(Strstr(one, "de") == NULL, 10);    /* Past end. */
  391.     check(Strstr(one, "") == one+4, 11);    /* Finding empty. */
  392.     /*void*/ Strcpy(one, "ababa");
  393.     check(Strstr(one, "ba") == one+1, 12);    /* Finding first. */
  394.     /*void*/ Strcpy(one, "");
  395.     check(Strstr(one, "b") == NULL, 13);    /* Empty String. */
  396.     check(Strstr(one, "") == one, 14);    /* Empty in empty String. */
  397.     /*void*/ Strcpy(one, "bcbca");
  398.     check(Strstr(one, "bca") == one+2, 15);    /* False start. */
  399.     /*void*/ Strcpy(one, "bbbcabbca");
  400.     check(Strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  401.  
  402.     /*
  403.      * Strspn
  404.      */
  405.     it = "Strspn";
  406.     check(Strspn("abcba", "abc") == 5, 1);    /* Whole String. */
  407.     check(Strspn("abcba", "ab") == 2, 2);    /* Partial. */
  408.     check(Strspn("abc", "qx") == 0, 3);    /* None. */
  409.     check(Strspn("", "ab") == 0, 4);    /* Null String. */
  410.     check(Strspn("abc", "") == 0, 5);    /* Null search list. */
  411.  
  412.     /*
  413.      * Strcspn
  414.      */
  415.     it = "Strcspn";
  416.     check(Strcspn("abcba", "qx") == 5, 1);    /* Whole String. */
  417.     check(Strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  418.     check(Strcspn("abc", "abc") == 0, 3);    /* None. */
  419.     check(Strcspn("", "ab") == 0, 4);    /* Null String. */
  420.     check(Strcspn("abc", "") == 3, 5);    /* Null search list. */
  421.  
  422.     /*
  423.      * Strtok - the hard one
  424.      */
  425.     it = "Strtok";
  426.     /*void*/ Strcpy(one, "first, second, third");
  427.     equal(Strtok(one, ", "), "first", 1);    /* Basic test. */
  428.     equal(one, "first", 2);
  429.     equal(Strtok((char *)NULL, ", "), "second", 3);
  430.     equal(Strtok((char *)NULL, ", "), "third", 4);
  431.     check(Strtok((char *)NULL, ", ") == NULL, 5);
  432.     /*void*/ Strcpy(one, ", first, ");
  433.     equal(Strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  434.     check(Strtok((char *)NULL, ", ") == NULL, 7);
  435.     /*void*/ Strcpy(one, "1a, 1b; 2a, 2b");
  436.     equal(Strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  437.     equal(Strtok((char *)NULL, "; "), "1b", 9);
  438.     equal(Strtok((char *)NULL, ", "), "2a", 10);
  439.     /*void*/ Strcpy(two, "x-y");
  440.     equal(Strtok(two, "-"), "x", 11);    /* New String before done. */
  441.     equal(Strtok((char *)NULL, "-"), "y", 12);
  442.     check(Strtok((char *)NULL, "-") == NULL, 13);
  443.     /*void*/ Strcpy(one, "a,b, c,, ,d");
  444.     equal(Strtok(one, ", "), "a", 14);    /* Different separators. */
  445.     equal(Strtok((char *)NULL, ", "), "b", 15);
  446.     equal(Strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  447.     equal(Strtok((char *)NULL, " ,"), "d", 17);
  448.     check(Strtok((char *)NULL, ", ") == NULL, 18);
  449.     check(Strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  450.     /*void*/ Strcpy(one, ", ");
  451.     check(Strtok(one, ", ") == NULL, 20);    /* No tokens. */
  452.     /*void*/ Strcpy(one, "");
  453.     check(Strtok(one, ", ") == NULL, 21);    /* Empty String. */
  454.     /*void*/ Strcpy(one, "abc");
  455.     equal(Strtok(one, ", "), "abc", 22);    /* No delimiters. */
  456.     check(Strtok((char *)NULL, ", ") == NULL, 23);
  457.     /*void*/ Strcpy(one, "abc");
  458.     equal(Strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  459.     check(Strtok((char *)NULL, "") == NULL, 25);
  460.     /*void*/ Strcpy(one, "abcdefgh");
  461.     /*void*/ Strcpy(one, "a,b,c");
  462.     equal(Strtok(one, ","), "a", 26);    /* Basics again... */
  463.     equal(Strtok((char *)NULL, ","), "b", 27);
  464.     equal(Strtok((char *)NULL, ","), "c", 28);
  465.     check(Strtok((char *)NULL, ",") == NULL, 29);
  466.     equal(one+6, "gh", 30);            /* Stomped past end? */
  467.     equal(one, "a", 31);            /* Stomped old tokens? */
  468.     equal(one+2, "b", 32);
  469.     equal(one+4, "c", 33);
  470.  
  471.     /*
  472.      * Memcmp
  473.      */
  474.     it = "Memcmp";
  475.     check(Memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  476.     check(Memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  477.     check(Memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  478.     check(Memcmp("abce", "abcd", 4) > 0, 4);
  479.     check(Memcmp("alph", "beta", 4) < 0, 5);
  480.     if (charsigned)                /* Sign-bit comparison. */
  481.         check(Memcmp("a\203", "a\003", 2) < 0, 6);
  482.     else
  483.         check(Memcmp("a\203", "a\003", 2) > 0, 6);
  484.     check(Memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  485.     check(Memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  486.  
  487.     /*
  488.      * Memchr
  489.      */
  490.     it = "Memchr";
  491.     check(Memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  492.     /*void*/ Strcpy(one, "abcd");
  493.     check(Memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  494.     check(Memchr(one, 'd', 4) == one+3, 3);    /* End of String. */
  495.     check(Memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  496.     check(Memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  497.     /*void*/ Strcpy(one, "ababa");
  498.     check(Memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  499.     check(Memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  500.     check(Memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  501.     /*void*/ Strcpy(one, "a\203b");
  502.     check(Memchr(one, (char)0203, 3) == one+1, 9);    /* Unsignedness. */
  503.  
  504.     /*
  505.      * Memcpy
  506.      *
  507.      * Note that X3J11 says Memcpy must work regardless of overlap.
  508.      * The SVID says it might fail.
  509.      */
  510.     it = "Memcpy";
  511.     check(Memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  512.     equal(one, "abc", 2);            /* Did the copy go right? */
  513.  
  514.     /*void*/ Strcpy(one, "abcdefgh");
  515.     /*void*/ Memcpy(one+1, "xyz", 2);
  516.     equal(one, "axydefgh", 3);        /* Basic test. */
  517.  
  518.     /*void*/ Strcpy(one, "abc");
  519.     /*void*/ Memcpy(one, "xyz", 0);
  520.     equal(one, "abc", 4);            /* Zero-length copy. */
  521.  
  522.     /*void*/ Strcpy(one, "hi there");
  523.     /*void*/ Strcpy(two, "foo");
  524.     /*void*/ Memcpy(two, one, 9);
  525.     equal(two, "hi there", 5);        /* Just paranoia. */
  526.     equal(one, "hi there", 6);        /* Stomped on source? */
  527.  
  528.     /*void*/ Strcpy(one, "abcdefgh");
  529.     /*void*/ Memcpy(one+1, one, 9);
  530.     equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  531.  
  532.     /*void*/ Strcpy(one, "abcdefgh");
  533.     /*void*/ Memcpy(one+1, one+2, 7);
  534.     equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  535.  
  536.     /*void*/ Strcpy(one, "abcdefgh");
  537.     /*void*/ Memcpy(one, one, 9);
  538.     equal(one, "abcdefgh", 9);        /* 100% overlap. */
  539.  
  540.     /*
  541.      * Memccpy - first test like Memcpy, then the search part
  542.      *
  543.      * The SVID, the only place where Memccpy is mentioned, says
  544.      * overlap might fail, so we don't try it.  Besides, it's hard
  545.      * to see the rationale for a non-left-to-right Memccpy.
  546.      */
  547.     it =(char *)"Memccpy";
  548.     check(Memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  549.     equal(one, "abc", 2);            /* Did the copy go right? */
  550.  
  551.     /*void*/ Strcpy(one, "abcdefgh");
  552.     /*void*/ Memccpy(one+1, "xyz", 'q', 2);
  553.     equal(one, "axydefgh", 3);        /* Basic test. */
  554.  
  555.     /*void*/ Strcpy(one, "abc");
  556.     /*void*/ Memccpy(one, "xyz", 'q', 0);
  557.     equal(one, "abc", 4);            /* Zero-length copy. */
  558.  
  559.     /*void*/ Strcpy(one, "hi there");
  560.     /*void*/ Strcpy(two, "foo");
  561.     /*void*/ Memccpy(two, one, 'q', 9);
  562.     equal(two, "hi there", 5);        /* Just paranoia. */
  563.     equal(one, "hi there", 6);        /* Stomped on source? */
  564.  
  565.     /*void*/ Strcpy(one, "abcdefgh");
  566.     /*void*/ Strcpy(two, "horsefeathers");
  567.     check(Memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  568.     equal(one, "abcdefgh", 8);        /* Source intact? */
  569.     equal(two, "abcdefeathers", 9);        /* Copy correct? */
  570.  
  571.     /*void*/ Strcpy(one, "abcd");
  572.     /*void*/ Strcpy(two, "bumblebee");
  573.     check(Memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  574.     equal(two, "aumblebee", 11);
  575.     check(Memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  576.     equal(two, "abcdlebee", 13);
  577.     /*void*/ Strcpy(one, "xyz");
  578.     check(Memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  579.     equal(two, "xbcdlebee", 15);
  580.  
  581.     /*
  582.      * Memset
  583.      */
  584.     it = "Memset";
  585.     /*void*/ Strcpy(one, "abcdefgh");
  586.     check(Memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  587.     equal(one, "axxxefgh", 2);        /* Basic test. */
  588.  
  589.     /*void*/ Memset(one+2, 'y', 0);
  590.     equal(one, "axxxefgh", 3);        /* Zero-length set. */
  591.  
  592.     /*void*/ Memset(one+5, '\0', 1);
  593.     equal(one, "axxxe", 4);            /* Zero fill. */
  594.     equal(one+6, "gh", 5);            /* And the leftover. */
  595.  
  596.     /*void*/ Memset(one+2, (char)010045, 1);
  597.     equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  598.  
  599.     /*
  600.      * Bcopy - much like Memcpy
  601.      *
  602.      * Berklix manual is silent about overlap, so don't test it.
  603.      */
  604.     it = "Bcopy";
  605.     /*void*/ Bcopy("abc", one, 4);
  606.     equal(one, "abc", 1);            /* Simple copy. */
  607.  
  608.     /*void*/ Strcpy(one, "abcdefgh");
  609.     /*void*/ Bcopy("xyz", one+1, 2);
  610.     equal(one, "axydefgh", 2);        /* Basic test. */
  611.  
  612.     /*void*/ Strcpy(one, "abc");
  613.     /*void*/ Bcopy("xyz", one, 0);
  614.     equal(one, "abc", 3);            /* Zero-length copy. */
  615.  
  616.     /*void*/ Strcpy(one, "hi there");
  617.     /*void*/ Strcpy(two, "foo");
  618.     /*void*/ Bcopy(one, two, 9);
  619.     equal(two, "hi there", 4);        /* Just paranoia. */
  620.     equal(one, "hi there", 5);        /* Stomped on source? */
  621.  
  622.     /*
  623.      * Bzero
  624.      */
  625.     it = "Bzero";
  626.     /*void*/ Strcpy(one, "abcdef");
  627.     Bzero(one+2, 2);
  628.     equal(one, "ab", 1);            /* Basic test. */
  629.     equal(one+3, "", 2);
  630.     equal(one+4, "ef", 3);
  631.  
  632.     /*void*/ Strcpy(one, "abcdef");
  633.     Bzero(one+2, 0);
  634.     equal(one, "abcdef", 4);        /* Zero-length copy. */
  635.  
  636.     /*
  637.      * Bcmp - somewhat like Memcmp
  638.      */
  639.     it = "Bcmp";
  640.     check(Bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  641.     check(Bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  642.     check(Bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  643.     check(Bcmp("abce", "abcd", 4) != 0, 4);
  644.     check(Bcmp("alph", "beta", 4) != 0, 5);
  645.     check(Bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  646.     check(Bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  647.  
  648. }
  649.